package fussbudget.gui.components.support;

import interfaces.IcolumnData;

import java.util.ArrayList;
import java.util.LinkedHashSet;

import javax.swing.table.AbstractTableModel;

import fussbudget.Address;
import fussbudget.PaymentEntity;

public class DefaultTablePanelModel<E extends IcolumnData> extends AbstractTableModel {
	protected ArrayList<String>	columnNames	= new ArrayList<String>();
	private LinkedHashSet<E>	data		= new LinkedHashSet<E>();

	public void addRow(E newEntity) {
		data.add(newEntity);
		int newRow = data.size() - 1;
		this.fireTableRowsInserted(newRow, newRow);
	}
	
	public String getColumnName(int col) {
		return columnNames.get(col);
	}

	@Override
	public int getColumnCount() {
		return columnNames.size();
	}

	public boolean isCellEditable(int row, int col) {
		return true;
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public Object getValueAt(int row, int col) {
		Object[] dataArray = data.toArray();
		E entity = (E)dataArray[row];
		return entity.getElement(getColumnName(col));
	}

	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	public void setValueAt(Object value, int row, int col) {
		Object[] dataArray = data.toArray();
		E entity = (E)dataArray[row];
		entity.setElement(getColumnName(col), value);

		fireTableCellUpdated(row, col);
	}

}
